package com.xique.system.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.system.bean.domain.SystemNotice;
import com.xique.system.constants.SystemConstants;
import com.xique.system.service.service.ISystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.page.TableDataInfo;

/**
 * 后台系统公告Controller
 *
 * @author xique
 * @date 2022-06-09
 */
@RestController
@RequestMapping("/notice")
public class SystemNoticeController extends BaseController {
    @Autowired
    private ISystemNoticeService systemNoticeService;

    /**
     * 查询后台系统公告列表
     */
//    @PreAuthorize(hasPermi = "park:notice:list")
    @GetMapping("/list")
    public TableDataInfo list(SystemNotice systemNotice) {
        startPage();
        List<SystemNotice> list = systemNoticeService.selectSystemNoticeList(systemNotice);
        Date date = new Date();
        list.stream().forEach(e -> {
            //状态（sys_notice_status，0启用1停用）,返回的状态为虚拟状态（判定条件：除去手动关闭状态+当前时间属于发布时间区间）
            if (SystemConstants.NoticeStatus.ON.equals(e.getStatus()) && date.after(e.getStartTime()) &&  date.before(e.getEndTime()) ){
                e.setStatus(SystemConstants.NoticeStatus.ON);
            }else {
                e.setStatus(SystemConstants.NoticeStatus.OFF);
            }

        });
        return getDataTable(list);
    }

//    @PreAuthorize(hasPermi = "park:notice:list")
    @GetMapping("/popup")
    public TableDataInfo popup(SystemNotice systemNotice) {
        startPage();
        systemNotice.setNowTime(new Date());
        systemNotice.setStatus(SystemConstants.NoticeStatus.ON);
        List<SystemNotice> list = systemNoticeService.selectSystemNoticeList(systemNotice);
        return getDataTable(list);
    }

    /**
     * 导出后台系统公告列表
     */
    @PreAuthorize(hasPermi = "park:notice:export")
    @Log(title = "后台系统公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemNotice systemNotice) throws IOException {
        List<SystemNotice> list = systemNoticeService.selectSystemNoticeList(systemNotice);
        ExcelUtil<SystemNotice> util = new ExcelUtil<SystemNotice>(SystemNotice.class);
        util.exportExcel(response, list, "后台系统公告数据");
    }

    /**
     * 获取后台系统公告详细信息
     */
//    @PreAuthorize(hasPermi = "park:notice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(systemNoticeService.selectSystemNoticeById(id));
    }

    /**
     * 新增后台系统公告
     */
    @PreAuthorize(hasPermi = "park:notice:add")
    @Log(title = "后台系统公告", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody SystemNotice systemNotice) {
        systemNotice.setCreateBy(SecurityUtils.getUsername());
        systemNotice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(systemNoticeService.insertSystemNotice(systemNotice));
    }

    /**
     * 修改后台系统公告
     */
    @PreAuthorize(hasPermi = "park:notice:edit")
    @Log(title = "后台系统公告", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update")
    public AjaxResult edit(@RequestBody SystemNotice request) {
        SystemNotice systemNotice = systemNoticeService.selectSystemNoticeById(request.getId());
        if (systemNotice.getEndTime().before(new Date()) ){
            return AjaxResult.error("已过期，无法启用！");
        }
        if ( systemNotice.getStartTime().after(new Date())){
            return AjaxResult.error("未到展示时间，到期自动启用！");

        }
        systemNotice.setStatus(request.getStatus());
        return toAjax(systemNoticeService.updateSystemNotice(systemNotice));
    }

    /**
     * 删除后台系统公告
     */
    @PreAuthorize(hasPermi = "park:notice:remove")
    @Log(title = "后台系统公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(systemNoticeService.deleteSystemNoticeByIds(ids));
    }
}
